aboutsummaryrefslogtreecommitdiff
path: root/src/routes/projects/[id]/+page.svelte
blob: 345f35f3b6b995e4a75dce9a2c90619224ea038a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

<script lang="ts">
	import { enhance } from '$app/forms';
	import { matchItems } from '$lib/components/fuzzy-search';
	import ScoreBadge from '$lib/components/ScoreBadge.svelte';
	import StatusBadge from '$lib/components/StatusBadge.svelte';
	import {
		formatFrequency,
		parseIntervalToDays,
		type RoutineState,
	} from '$lib/shared/routine-metrics';

	let { data, form } = $props();

	let name = $state('');
	let frequency = $state('');
	let editingId = $state<number | null>(null);
	let editName = $state('');
	let editFrequency = $state('');

	const frequencyDays = $derived(parseIntervalToDays(frequency));
	const editFrequencyDays = $derived(parseIntervalToDays(editFrequency));

	let showArchived = $state(false);
	let archiveFilter = $state('');

	const filteredArchived = $derived(
		archiveFilter.trim() === ''
			? data.archived
			: matchItems(data.archived, archiveFilter).map((m) => m.obj),
	);

	const dateFormatter = new Intl.DateTimeFormat('fr-FR', {
		day: 'numeric',
		month: 'short',
	});

	function sorted(routines: RoutineState[]): RoutineState[] {
		return [...routines].sort(
			(a, b) => b.overdueRatio - a.overdueRatio || a.name.localeCompare(b.name),
		);
	}

	function startEdit(routine: RoutineState) {
		editingId = routine.id;
		editName = routine.name;
		editFrequency = `${routine.frequencyDays}d`;
	}
</script>

<main>
	<header class="page-header">
		<h1>{data.project.name}</h1>
		<ScoreBadge score={data.healthScore} />
	</header>

	<form
		class="add-bar"
		method="POST"
		action="?/addRoutine"
		use:enhance={() => {
			return async ({ update }) => {
				name = '';
				frequency = '';
				await update({ reset: false });
			};
		}}
	>
		<input
			class="form-input"
			type="text"
			name="name"
			bind:value={name}
			placeholder="Nouvelle routine..."
			autocomplete="off"
		/>
		<input
			class="form-input frequency-input"
			class:input-invalid={frequency !== '' && frequencyDays === null}
			type="text"
			name="frequencyDays"
			bind:value={frequency}
			placeholder="2w"
			autocomplete="off"
			aria-label="Intervalle (ex: 1d, 2w, 3m)"
		/>
		<button
			class="btn-accent"
			type="submit"
			disabled={!name.trim() || frequencyDays === null}
			aria-label="Ajouter"
		>
			+
		</button>
	</form>

	{#if frequency !== '' && frequencyDays === null}
		<p class="error">Intervalle invalide — ex : 1d, 2w, 3m, 1y</p>
	{/if}

	{#if form?.error}
		<p class="error">{form.error}</p>
	{/if}

	<ul class="item-list">
		{#each sorted(data.routines) as routine (routine.id)}
			<li class="item-row item-row--card routine-row">
				{#if editingId === routine.id}
					<form
						class="edit-form"
						method="POST"
						action="?/editRoutine"
						use:enhance={() => {
							return async ({ result, update }) => {
								if (result.type === 'failure') {
									// Keep the edit form open so the error is visible.
									await update({ reset: false });
									return;
								}
								editingId = null;
								await update({ reset: false });
							};
						}}
					>
						{#if form?.error}
							<p class="error edit-error">{form.error}</p>
						{/if}
						<input type="hidden" name="id" value={routine.id} />
						<input
							class="form-input"
							type="text"
							name="name"
							bind:value={editName}
						/>
						<input
							class="form-input frequency-input"
							class:input-invalid={editFrequency !== '' &&
								editFrequencyDays === null}
							type="text"
							name="frequencyDays"
							bind:value={editFrequency}
							placeholder="2w"
							autocomplete="off"
							aria-label="Intervalle (ex: 1d, 2w, 3m)"
						/>
						<button
							class="btn-primary"
							type="submit"
							disabled={!editName.trim() || editFrequencyDays === null}
						>
							OK
						</button>
						<button
							class="btn-ghost"
							type="button"
							onclick={() => (editingId = null)}>✕</button
						>
					</form>
				{:else}
					<div class="routine-info">
						<a
							class="routine-name"
							href="/projects/{data.project.id}/routine/{routine.id}"
							>{routine.name}</a
						>
						<span class="routine-meta">
							{formatFrequency(routine.frequencyDays)}
							{#if routine.lastDoneAt}
								· fait le {dateFormatter.format(new Date(routine.lastDoneAt))}
							{:else}
								· jamais fait
							{/if}
						</span>
					</div>
					<StatusBadge
						status={routine.status}
						overdueDays={routine.overdueDays}
					/>
					<form method="POST" action="?/complete" use:enhance>
						<input type="hidden" name="id" value={routine.id} />
						<button class="btn-done" type="submit">Fait</button>
					</form>
					<button
						class="btn-ghost"
						type="button"
						aria-label="Modifier"
						onclick={() => startEdit(routine)}>✎</button
					>
					<form method="POST" action="?/archiveRoutine" use:enhance>
						<input type="hidden" name="id" value={routine.id} />
						<button class="btn-ghost" type="submit" aria-label="Archiver">
							&times;
						</button>
					</form>
				{/if}
			</li>
		{/each}
	</ul>

	{#if data.routines.length === 0}
		<p class="empty">Aucune routine</p>
	{/if}

	{#if data.archived.length > 0}
		<section class="archived-section">
			<button
				class="archived-toggle"
				type="button"
				onclick={() => (showArchived = !showArchived)}
				aria-expanded={showArchived}
			>
				<span class="chevron">{showArchived ? '▾' : '▸'}</span>
				Archivées
				<span class="count">{data.archived.length}</span>
			</button>
			{#if showArchived}
				<input
					class="form-input archived-search"
					type="search"
					bind:value={archiveFilter}
					placeholder="Rechercher une routine archivée..."
					autocomplete="off"
					aria-label="Rechercher parmi les routines archivées"
				/>
				<ul class="item-list">
					{#each filteredArchived as routine (routine.id)}
						<li class="item-row item-row--card archived-row">
							<a
								class="routine-name"
								href="/projects/{data.project.id}/routine/{routine.id}"
								>{routine.name}</a
							>
							<span class="archived-date">
								{formatFrequency(routine.frequencyDays)} · archivée le
								{dateFormatter.format(new Date(routine.archivedAt))}
							</span>
						</li>
					{/each}
				</ul>
				{#if filteredArchived.length === 0}
					<p class="empty archived-empty">Aucune correspondance.</p>
				{/if}
			{/if}
		</section>
	{/if}

	<footer class="page-footer">
		<form method="POST" action="?/archiveProject" use:enhance>
			<button class="btn-archive" type="submit">Archiver le projet</button>
		</form>
	</footer>
</main>

<style>
	.routine-row {
		flex-wrap: wrap;
		padding: 0.75rem 1rem;
	}

	.routine-info {
		flex-grow: 1;
		min-width: 0;
		display: flex;
		flex-direction: column;
		line-height: 1.2;
	}

	.routine-name {
		font-weight: 500;
		color: var(--text);
		text-decoration: none;
	}

	.routine-name:hover {
		color: var(--accent);
	}

	.routine-meta {
		font-size: 0.75rem;
		color: var(--text-secondary);
	}

	.edit-form {
		display: flex;
		flex-grow: 1;
		gap: 0.5rem;
		align-items: center;
		flex-wrap: wrap;
	}

	.edit-error {
		width: 100%;
	}

	.edit-form .form-input {
		flex: 1;
		padding: 0.5rem 0.75rem;
	}

	.frequency-input {
		width: 4.5rem;
		flex-shrink: 0;
	}

	.page-footer {
		margin-top: 2rem;
		display: flex;
		justify-content: center;
	}

	.btn-archive {
		border: none;
		background: transparent;
		color: var(--text-secondary);
		font-size: 0.875rem;
		text-decoration: underline;
		cursor: pointer;
	}

	.btn-archive:hover {
		color: var(--danger);
	}

	.archived-section {
		margin-top: 1.5rem;
		display: flex;
		flex-direction: column;
		gap: 0.75rem;
	}

	.archived-toggle {
		align-self: flex-start;
		display: inline-flex;
		align-items: center;
		gap: 0.5rem;
		padding: 0.375rem 0.75rem;
		border: none;
		border-radius: 0.625rem;
		background: transparent;
		color: var(--text-secondary);
		font-family: inherit;
		font-size: 0.875rem;
		font-weight: 500;
		cursor: pointer;
		transition: all 0.2s ease;
	}

	.archived-toggle:hover {
		background: var(--surface);
		color: var(--text);
	}

	.archived-toggle .chevron {
		font-size: 0.75rem;
	}

	.archived-toggle .count {
		min-width: 1.25rem;
		padding: 0 0.375rem;
		border-radius: 9999px;
		background: var(--surface);
		text-align: center;
		font-size: 0.75rem;
		font-weight: 600;
	}

	.archived-search {
		width: 100%;
	}

	.archived-row .routine-name {
		flex-grow: 1;
		color: var(--text-secondary);
	}

	.archived-row .routine-name:hover {
		color: var(--accent);
	}

	.archived-date {
		flex-shrink: 0;
		font-size: 0.75rem;
		color: var(--text-secondary);
	}

	.archived-empty {
		padding: 1rem 0;
	}
</style>